package com.qf.business.goods.controller;

import com.qf.business.goods.service.GoodsFenleiGuigeService;
import com.qf.business.goods.service.GoodsGuigeService;
import com.qf.business.goods.service.GoodsGuigeValService;
import com.qf.data.base.r.R;
import com.qf.data.base.r.Rs;
import com.qf.data.base.utils.QfBeanUtils;
import com.qf.data.goods.entity.GoodsFenleiGuige;
import com.qf.data.goods.entity.GoodsGuige;
import com.qf.data.goods.entity.GoodsGuigeVal;
import com.qf.data.goods.vo.input.GuigeInput;
import com.qf.data.goods.vo.output.GuigeOutput;
import com.qf.data.goods.vo.output.GuigeSelectOutput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@RestController
@RequestMapping("/guige")
@Slf4j
public class GuigeController {

    @Autowired
    private GoodsGuigeService goodsGuigeService;

    @Autowired
    private GoodsGuigeValService guigeValServicel;

    @Autowired
    private GoodsFenleiGuigeService fenleiGuigeService;

    /**
     * 查询规格列表 - 分页
     * @return
     */
    @RequestMapping("/list")
    public R list(){
//        List<GoodsGuige> guiges = goodsGuigeService.list();
        List<GuigeOutput> guigeOutputs = goodsGuigeService.queryList();
        return Rs.create(guigeOutputs);
    }

    /**
     * 设置规格时，查询规格列表的接口
     * @return
     */
    @RequestMapping("/select/list")
    public R selectlist(){
        List<GoodsGuige> guiges = goodsGuigeService.list();
        //将原本的规格实体 通过stream流 逐个的转换成GuigeSelectOutput 对象 生成一个集合，返回可客户端
        List<GuigeSelectOutput> selectList = guiges.stream().map(goodsGuige -> {
            GuigeSelectOutput guigeSelectOutput = new GuigeSelectOutput();
            guigeSelectOutput.setKey(goodsGuige.getId());
            guigeSelectOutput.setLabel(goodsGuige.getGuigeName());
            return guigeSelectOutput;
        }).collect(Collectors.toList());

        return Rs.create(selectList);
    }

    /**
     * 新增规格
     * @return
     */
    @RequestMapping("/insert")
    public R insert(@Valid GuigeInput input){
        GoodsGuige goodsGuige = QfBeanUtils.beanCopy(input, GoodsGuige.class);
        goodsGuigeService.save(goodsGuige);
        return Rs.create(true);
    }

    /**
     * 查询指定规格的数据列表
     * @return
     */
    @RequestMapping("/list/vals")
    public R listVals(Long guigeId){
        List<GoodsGuigeVal> vals = goodsGuigeService.queryValList(guigeId);
        return Rs.create(vals);
    }

    /**
     * 查询指定分类下 拥有个规格id集合
     * @return
     */
    @RequestMapping("/has/list")
    public R queryFenleiHasGuige(Long fenleiId){
        List<GoodsFenleiGuige> hasGuiges = fenleiGuigeService.query().eq("fenlei_id", fenleiId).list();
        List<Long> guigeIds = hasGuiges.stream()
                .map(goodsFenleiGuige -> goodsFenleiGuige.getGuigeId())
                .collect(Collectors.toList());
        return Rs.create(guigeIds);
    }

    /**
     * 新增规格数据
     * @return
     */
    @RequestMapping("/insert/vals")
    public R insertVals(Long guigeId, String guigeVal){
        GoodsGuigeVal goodsGuigeVal = new GoodsGuigeVal();
        goodsGuigeVal.setGuigeId(guigeId);
        goodsGuigeVal.setGuigeVal(guigeVal);
        guigeValServicel.save(goodsGuigeVal);
        return Rs.create(true);
    }


    /**
     * 查询规格列表以及规格对应的数据列表 根据 分类id
     * @return
     */
    @RequestMapping("/listAndVals")
    public R listAndVals(Long fid){
        log.info("[guige vals query] - 根据分类查询规格以及规则值的列表 - {}", fid);
        List<GuigeOutput> vals = goodsGuigeService.queryListByFid(fid);
        return Rs.create(vals);
    }
}
